WordWrapIndex = $E9 Position in the buffer where the text that needs wrapping is BufferRead = $EA (16-bit) Text window buffer read position for moves BufferWrite = $EC (16-bit) Text window buffer write position for moves MoveLength = $EE TextBufferIndex = $0163 Position to next insert text in buffer TextBuffer = $04A0 (64 bytes. In the original game it was 16-char x 4 lines in the translation it is (24 char + 8 overflow) x 3 lines I think the first line of a string might have been limited by 1 less character as a "stop" marker at the beginning for when the game scrolls the text window up.) This routine expects that text in the RAM buffer will be formatted as such: AAAAAAAAAAAAAAAA AAAAAAAA........ BBBBBBBBBBBBBBBB BBBBBBBB........ CCCCCCCCCCCCCCCC CCCCCCCC........ Where A, B and C are lines A, B and C on the screen. (24 char by 3 lines) If it isn't, then it will detect the start of the word and shift that and everything else down. Character map: $FB Toggles Auto-line break $FC The original game's space code $FD Do word wrap $FE New space code $FF End of text code lda MoveLength bne ScrollRoutine ldx #$00 ;text buffer index 0 CheckFormats: lda TextBuffer,x cmp #$FF ;end string flag beq EndFormatAdjust cmp #$FD ;line break flag beq DoWordWrap cmp #$FB ;auto-break flag toggle beq UseScrollingText inx cpx #$60 bne CheckFormats stx TextBufferIndex EndFormatAdjust: rts UseScrollingText: lda #$FE ;replace Scroll Toggle flag with hard-space sta TextBuffer,x inx lda MoveLength beq SetScrollFlagOn lda #$00 sta MoveLength beq CheckFormats ;a=0, so this will branch always to CheckFormats, next iteration of loop SetScrollFlagOn: lda #$01 sta MoveLength bne CheckFormats ;a=1, so this will branch always to the the next iteration of the loop DoWordWrap: lda #$FE sta TextBuffer,x ;replace line break with hard space lda TextBuffer+0x19 ;check if character 25 of line 1 is hard-space already cmp #$FE beq CheckLine1B ;if it is, skip wrapping line 1 ldx #$19 jsr WordWrap beq WrapLine2 CheckLine1B: lda TextBuffer+0x1A cmp #$FE beq WrapLine2 ldx #$1A jsr WordWrap WrapLine2: lda TextBuffer+0x39 cmp #$FE beq CheckLine2B ldx #$39 jsr WordWrap beq EndWordWrap CheckLine2B: lda TextBuffer+0x3A cmp #$FE beq EndWordWrap ldx #$3A jsr WordWrap EndWordWrap: rts WordWrap: lda TextBuffer,x cmp #$FE ;check if we have a hard space beq CheckForEOL dex bne WordWrap ;keep going back until we find the last hard-space beq EndFormatAdjust ;if we get to the start of the window, stop CheckForEOL: txa pha ;backup index of space lda #$00 sta MoveLength ;initialize Move Length FindEOL: inx txa and #$1F beq MoveText ;if the text spilled over a line, then we need to break it lda TextBuffer,x cmp #$FE ;current text byte a space? Check if it's the EOL. beq IsItEOL inc MoveLength bne FindEOL ;since text buffer isn't 256 chars long, we can use this as branch always IsItEOL: inx lda TextBuffer,x cmp #$FE beq MoveText ;assuming 2 spaces in a row is probably an EOL. Move all text forward MoveLength bytes inc MoveLength ;it wasn't, so let's assume a space and continue checking dex bne FindEOL MoveText: pla ;retrieve index of space character sta WordWrapIndex and #$60 ;A = start of line index is on clc adc #$C0 ;convert to address in buffer tay ;Y = index of beginning of line with space ldx #$00 lda #$FF sta BufferWrite ;Write to $04FF (last byte of buffer) sec sbc MoveLength sta BufferRead ;Read from $04FF - MoveLength lda #04 sta BufferWrite+1 sta BufferRead+1 ;setting the high bytes of the pointers MoveTextLoop: lda (BufferRead,x) sta (BufferWrite,x) dec BufferRead dec BufferWrite ;transfer string backwards cpy BufferRead bcs MoveTextLoop ;repeat until we've copied through the current line inc BufferRead lda BufferRead sta BufferWrite ;we're going to insert text and the current point lda WordWrapIndex clc adc #$A0 ;low byte of address of TextBuffer sta BufferRead ;set read position to where the text is that needs to be word-wrapped WriteWrapText: lda (BufferRead,x) sta (BufferWrite,x) lda #$FE sta (BufferRead,x) ;now let's delete the string inc BufferRead inc BufferWrite dec MoveLength lda MoveLength bne WriteWrapText rts